// src/api/product.ts
import request from '@/utils/request'

export interface ProductPayload {
  id?: number
  barcode: string
  brand: string
  enabled: boolean
  memberPrice: number
  name: string
  price: number
  skuCode: string
  spec: string
  spuCode: string
  taxRate: number
  unit: string
  weighted: boolean
}

/**
 * 商品列表
 * GET /api/product/list
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":[...]}
 */
export function listProducts(): Promise<any> {
  return request({
    url: '/api/product/list',
    method: 'get',
  })
}

/**
 * 新增商品
 * POST /api/product
 * @param {Object} data
 * @param {string} data.barcode
 * @param {string} data.brand
 * @param {boolean} data.enabled
 * @param {number} data.memberPrice
 * @param {string} data.name
 * @param {number} data.price
 * @param {string} data.skuCode
 * @param {string} data.spec
 * @param {string} data.spuCode
 * @param {number} data.taxRate
 * @param {string} data.unit
 * @param {boolean} data.weighted
 * @returns {Promise} 响应：{"code":0,"message":"success","data":1}
 */
export function createProduct(data: ProductPayload): Promise<any> {
  return request({
    url: '/api/product',
    method: 'post',
    data,
  })
}

/**
 * 修改商品
 * PUT /api/product
 * @param {Object} data
 * @param {number} data.id
 * @param {string} data.barcode
 * @param {string} data.brand
 * @param {boolean} data.enabled
 * @param {number} data.memberPrice
 * @param {string} data.name
 * @param {number} data.price
 * @param {string} data.skuCode
 * @param {string} data.spec
 * @param {string} data.spuCode
 * @param {number} data.taxRate
 * @param {string} data.unit
 * @param {boolean} data.weighted
 * @returns {Promise} 响应：{"code":0,"message":"success","data":1}
 */
export function updateProduct(data: ProductPayload): Promise<any> {
  return request({
    url: '/api/product',
    method: 'put',
    data,
  })
}

/**
 * 删除商品
 * DELETE /api/product/{id}
 * @param {number|string} id 商品ID
 * @returns {Promise} 响应：{"code":0,"message":"success","data":1}
 */
export function deleteProduct(id: number | string): Promise<any> {
  return request({
    url: `/api/product/${id}`,
    method: 'delete',
  })
}